import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { User } from '../user/user.entity';

@Entity('books')
export class Book {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  title: string;

  @Column()
  author: string;

  @Column()
  isbn: string;

  @Column('text')
  description: string;

  @Column()
  cover: string;

  @Column()
  publisher: string;

  @Column()
  ownerphone: string;

  @Column()
  owneraddress: string;

  @Column()
  publishDate: Date;

  @Column()
  category: string;

  @Column()
  status: 'available' | 'borrowed' | 'reserved';

  @Column({ nullable: true })
  currentBorrowerId: number | null;

  @Column({ nullable: true })
  reservedId: number | null;

  @Column({ type: 'datetime', nullable: true })
  returnDate: Date | null;

  @Column()
  ownerId: number;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'ownerId' })
  owner: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'reservedId' })
  reserveder: User;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'currentBorrowerId' })
  currentBorrower: User;

  @OneToMany(() => BookComment, (comment) => comment.book)
  comments: BookComment[];

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}

@Entity('book_comments')
export class BookComment {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('text')
  content: string;

  @Column()
  bookId: number;

  @Column()
  userId: number;

  @ManyToOne(() => Book, (book) => book.comments)
  @JoinColumn({ name: 'bookId' })
  book: Book;

  @ManyToOne(() => User)
  @JoinColumn({ name: 'userId' })
  user: User;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;
}
